/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package reminder;

import abstractmodel.*;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;
import javax.mail.internet.*;

/**
 *
 * @author littlechild
 */
public class Utilities {
    public static boolean sendEmail(String emailAddress,String subject,String body) {
        try {
            String to = emailAddress;
            String from = "smallpie.iWallet@gmail.com";
            String host = "smtp.gmail.com";
            Properties pro = new Properties();
            pro.setProperty("mail.smtp.host", host);
            pro.put("mail.smtps.auth", true);

            Session s = Session.getInstance(pro, null);
            s.setDebug(false);

            MimeMessage message = new MimeMessage(s);
            message.setFrom(new InternetAddress(from));
            InternetAddress[] address = new InternetAddress[]{new InternetAddress(to)};
            message.setRecipients(Message.RecipientType.TO, address);
            message.setSubject(subject);
            message.setContent(body, "text/plain");
            Transport t = s.getTransport("smtps");
            
            t.connect(host, from,"zxfxjyyx");
            t.sendMessage(message,message.getAllRecipients());
            t.close();
        } catch (MessagingException ex) {
            //Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        return true;
    }
    public static IAccount searchForAccount(IUser user,int account_id) {
        ArrayList<IBook> books=user.getBooks();
        IAccount account;
        for(IBook book :books) {
            account=searchForAccount(book,account_id);
            if(account!=null)
                return account;
        }
        return null;
    }
    public static IAccount searchForAccount(IBook book, int id) {
        ArrayList<IAccount> accounts=book.getAccounts();
        for(IAccount account: accounts) {
            if(account.getAccountInfo().getid()==id)
                return account;
        }
        return null;
    }
}
